﻿using System;
using System.Collections.Generic;

using Pixels.Logic;

using Ur;
using Sargon;

namespace Pixels
{
    /// <summary> All game data goes here. </summary>
    static public class GlobalConsts {

        static private TerrainConsts[]      terrainConsts;
        static private StructureConsts[]    structureConsts;
        static private Technology[]         technologies;
        static private ResourceConsts[]     resources;

        static public ResourcePool startingResources;

        public const int SPACE_PER_HABITAT_MODULE = 6;

        static public TerrainConsts getTerrainConsts(TerrainType terrain) {
            return terrainConsts[(int)terrain];
        }

        static public StructureConsts getStructureConsts(StructureType structure) {
            return structureConsts[(int)structure];
        }

        static public Technology getTechnology(Technologies techID) {
            return technologies[(int)techID];
        }

        static public ResourceConsts getResourceConsts(Resource res) {
            return resources[(int)res];
        }

        static public void initializeAll() {

            terrainConsts = new TerrainConsts[(int)TerrainType.ubound];

            terrainConsts[(int)TerrainType.rocks] = new TerrainConsts(
                TerrainType.rocks,
                "Rocks",
                Sargon.Color.grey(0.5f)
            );

            terrainConsts[(int)TerrainType.fertile] = new TerrainConsts(
                TerrainType.fertile,
                "Fertile",
                Sargon.Color.white
            );

            terrainConsts[(int)TerrainType.mushrooms] = new TerrainConsts(
                TerrainType.mushrooms,
                "Mushrooms",
                Sargon.Color.white
            );

            terrainConsts[(int)TerrainType.crystals] = new TerrainConsts(
                TerrainType.crystals,
                "Crystals",
                Sargon.Color.white
            );

            structureConsts = new StructureConsts[(int)StructureType.ubound];

            structureConsts[(int)StructureType.commandModule]   = new StructureConsts(StructureType.commandModule,      "CommandModule",    -1, new Color(1,1, 0.3f, 1f));
            structureConsts[(int)StructureType.habitatModule]   = new StructureConsts(StructureType.habitatModule,      "HabitatModule",    3,  new Color(0,1,1, 1f));
            structureConsts[(int)StructureType.hydroponicFarm]  = new StructureConsts(StructureType.hydroponicFarm,     "HydroponicFarm",   3,  new Color(0,1,0,1f));
            structureConsts[(int)StructureType.technoForge]     = new StructureConsts(StructureType.technoForge,        "TechnoForge",      3,  new Color(0,0,1,1f));
            structureConsts[(int)StructureType.solarCollector]  = new StructureConsts(StructureType.solarCollector,     "SolarCollector",   3,  new Color(1,1,0,1f));
            structureConsts[(int)StructureType.nuclearReactor]  = new StructureConsts(StructureType.nuclearReactor,     "NuclearReactor",   3,  new Color(1,0,0,1f));
            structureConsts[(int)StructureType.constructionSite] = new StructureConsts(StructureType.constructionSite,  "ConstructionSite", 3,  new Color(1, 0.5f, 0, 0.7f));
            structureConsts[(int)StructureType.antenna]         = new StructureConsts(StructureType.antenna,            "Antenna",          20, new Color(1, 1,1, 1));
            structureConsts[(int)StructureType.laboratory]      = new StructureConsts(StructureType.laboratory,         "Laboratory",       3,  new Color(1, 1,1, 1));
            structureConsts[(int)StructureType.concrete]        = new StructureConsts(StructureType.concrete,           "Concrete",         1,  new Color(1,1,1,1));
            structureConsts[(int)StructureType.bunker]          = new StructureConsts(StructureType.bunker, "Bunker", 1, new Color(1, 1, 1, 1));

            // assign structure costs : 
            getStructureConsts(StructureType.habitatModule).constructionCost.changeResource(Resource.Cogs, 2);
            getStructureConsts(StructureType.habitatModule).constructionCost.changeResource(Resource.Power, 1);
            getStructureConsts(StructureType.hydroponicFarm).constructionCost.changeResource(Resource.Cogs, 2);
            //getStructureConsts(StructureType.hydroponicFarm).constructionCost.changeResource(Resource.Apples, 1);
            getStructureConsts(StructureType.technoForge).constructionCost.changeResource(Resource.Cogs, 3);
            getStructureConsts(StructureType.technoForge).constructionCost.changeResource(Resource.Power, 2);
            getStructureConsts(StructureType.solarCollector).constructionCost.changeResource(Resource.Cogs, 2);
            getStructureConsts(StructureType.nuclearReactor).constructionCost.changeResource(Resource.Special, 1);
            getStructureConsts(StructureType.nuclearReactor).constructionCost.changeResource(Resource.Cogs, 5);
            getStructureConsts(StructureType.antenna).constructionCost.changeResource(Resource.Cogs, 30);
            getStructureConsts(StructureType.antenna).constructionCost.changeResource(Resource.Power,20);
            getStructureConsts(StructureType.bunker).constructionCost.changeResource(Resource.Cogs, 20);
            
            // starting resources of colony : 
            startingResources = new ResourcePool();
            startingResources.changeResource(Resource.Apples, 36);
            startingResources.changeResource(Resource.Cogs, 10);
            startingResources.changeResource(Resource.Power, 10);



            getTerrainConsts(TerrainType.rocks).tileTextures = new[] { "rocks", "fertile", "crystal" };

            technologies = new Technology[(int)Technologies.ubound];


            technologies[(int)Technologies.none] = new Technology(Technologies.none, "No Research");
            technologies[(int)Technologies.advanced_engineering] = new Technology(Technologies.advanced_engineering, "Advanced Engineering",    "Increases number of technicians per technoforge");
            technologies[(int)Technologies.electrified_fence]    = new Technology(Technologies.electrified_fence,    "Electrified Fence",       "Surplus power illuminates the fence at night", cost : 5);
            technologies[(int)Technologies.advanced_hydroponics] = new Technology(Technologies.advanced_hydroponics, "Advanced Hydroponics",    "Increases number of farmers per hydroponic farm");
            technologies[(int)Technologies.alien_biology]        = new Technology(Technologies.alien_biology,        "Alien Biochemistry",      "Can release deadly toxins" );
            technologies[(int)Technologies.alien_corpse]         = new Technology(Technologies.alien_corpse,         "Alien Corpse",            "Gain insight into alien anatomy" );
            technologies[(int)Technologies.crystal_refraction]   = new Technology(Technologies.crystal_refraction,   "Crystal Refraction",      "Solar Panels next to Crystals generate more power" );
            technologies[(int)Technologies.frequency_analysis]   = new Technology(Technologies.frequency_analysis,   "Frequency Analysis",      "Maybe we can get off this rock?" );
            technologies[(int)Technologies.pulse_rifles]         = new Technology(Technologies.pulse_rifles,         "Pulse Rifles",            "Increase soldier firepower");
            technologies[(int)Technologies.weather_patterns]     = new Technology(Technologies.weather_patterns,     "Weather Patterns",        "Laboratories can forecast weather");


            getTechnology(Technologies.electrified_fence).allow();
            getTechnology(Technologies.pulse_rifles).allow();
            getTechnology(Technologies.weather_patterns).allow();
            getTechnology(Technologies.alien_corpse).allow();

            //getTechnology(Technologies.pulse_rifles).resourcesNeededToStartResearch.changeResource(Resource.Special, 1);


            resources = new ResourceConsts[(int)Resource.Max];

            resources[(int)Resource.Apples] = new ResourceConsts(Resource.Apples, "Organics", "icon_food", Color.white);
            resources[(int)Resource.Cogs ]  = new ResourceConsts(Resource.Cogs, "Components", "icon_cog", Color.white);
            resources[(int)Resource.Power] = new ResourceConsts(Resource.Power, "Power",      "icon_power", Color.white);
            resources[(int)Resource.Special] = new ResourceConsts(Resource.Special, "Antimatter", "icon_special", Color.white);

            unfreezeCost = new ResourcePool();
            unfreezeCost.changeResource(Resource.Power, 10);
            unfreezeCost.changeResource(Resource.Cogs, 3);

            radioCost = new ResourcePool();
            radioCost.changeResource(Resource.Power, 10);
            radioCost.changeResource(Resource.Cogs,  10);

            getStructureConsts(StructureType.constructionSite).description = "Assign technicians to construct here";
            getStructureConsts(StructureType.laboratory).description = "Drag technicians to produce research. Drag colonists to convert into technicians";
            getStructureConsts(StructureType.technoForge).description = "Drag technicians to produce Components here.";
            getStructureConsts(StructureType.solarCollector).description = "Collects energy at a meager rate";
            getStructureConsts(StructureType.antenna).description = "Perhaps this could be our way out of here?";
            getStructureConsts(StructureType.bunker).description = "Drag civilians to train into soldiers";
            getStructureConsts(StructureType.commandModule).description = "Crashed spaceship. Still has a sinle operational reactor.";
            getStructureConsts(StructureType.hydroponicFarm).description = "Drag colonists to produce food";
            getStructureConsts(StructureType.nuclearReactor).description = "It hums ominously";

        }

        static public ResourcePool unfreezeCost;
        static public ResourcePool radioCost;

        public const int RESOURCES_PER_FARMER = 2;
        public const int COLONIST_DEATH_CHANCE_ADJACENT_TO_BLAST = 50;


        public static int GetSoldierFirepower { get {
            var value = 3;
            if (Technologies.pulse_rifles.Researched()) value += 3;
            if (Technologies.alien_corpse.Researched()) value += 1;
            if (Technologies.alien_biology.Researched()) value += 1;
            return value;
        }}

        public const int NUCLEAR_REACTOR_POWER_OUTPUT = 10;
    }
}
